"""
@File    :   out.py
@Time    :   2024/03/25 14:07:22
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   Token相关的pydantic序列化模型
"""

from datetime import datetime

from pydantic import BaseModel, Field, TypeAdapter

from src.business.admin.system.oauth2.auth.enums import OAuth2GrantTypeEnum
from src.business.admin.system.oauth2.token.enums import TokenTypeEnum
from src.schemas.out import BaseOutSchemaConfigMixin


class RefreshTokenCompleteOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """刷新令牌完整的序列化模型"""

    id: int = Field(default=..., description="主键")
    tenant_id: int = Field(default=..., description="租户ID")
    tenant_is_system: bool = Field(default=..., description="是否系统级别租户")
    tenant_pakcage_id: int = Field(default=..., description="租户套餐ID")
    create_datetime: datetime = Field(default=..., description="创建时间")
    expiration_datetime: datetime = Field(default=..., description="过期时间")
    oauth2_client_id: int = Field(default=..., description="OAuth2客户端ID")
    oauth2_grant_type: OAuth2GrantTypeEnum = Field(
        default=..., description="OAuth2授权方式"
    )
    oauth2_scopes: list[str] = Field(default=..., description="OAuth2授权范围")
    user_id: int = Field(default=..., description="用户ID")
    username: str = Field(default=..., description="用户名")
    user_is_admin: bool = Field(default=..., description="是否管理员用户")
    token_type: TokenTypeEnum = Field(default=..., description="令牌类型")
    refresh_token: str = Field(default=..., description="刷新令牌")


class AccessTokenCompleteOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """访问令牌完整的序列化模型"""

    id: int = Field(default=..., description="主键")
    tenant_id: int = Field(default=..., description="租户ID")
    tenant_is_system: bool = Field(default=..., description="是否系统级别租户")
    tenant_pakcage_id: int = Field(default=..., description="租户套餐ID")
    create_datetime: datetime = Field(default=..., description="创建时间")
    expiration_datetime: datetime = Field(default=..., description="过期时间")
    oauth2_client_id: int = Field(default=..., description="OAuth2客户端ID")
    oauth2_grant_type: OAuth2GrantTypeEnum = Field(
        default=..., description="OAuth2授权方式"
    )
    oauth2_scopes: list[str] = Field(default=..., description="OAuth2授权范围")
    user_id: int = Field(default=..., description="用户ID")
    username: str = Field(default=..., description="用户名")
    user_is_admin: bool = Field(default=..., description="是否管理员用户")
    token_type: TokenTypeEnum = Field(default=..., description="令牌类型")
    access_token: str = Field(default=..., description="访问令牌")
    refresh_token: str = Field(default=..., description="关联的刷新令牌")


# 完整的访问令牌序列化列表模型
ListAccessTokenCompleteOutSchema = TypeAdapter(list[AccessTokenCompleteOutSchema])


class DoubleTokenCompleteOutSchema(BaseModel, BaseOutSchemaConfigMixin):
    """双Token完整的序列化"""

    access_token: AccessTokenCompleteOutSchema
    refresh_token: RefreshTokenCompleteOutSchema
